package com.emokili.ewa.portal.interceptor;

import com.emokili.ewa.core.frame.entity.MemberEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.PrintWriter;

import static com.emokili.ewa.portal.constant.Constants.LOGIN_USER;

/**
 * 登录拦截器
 *
 * @author yuepanpan
 */
@Component
public class LoginInterceptor extends HandlerInterceptorAdapter {
    public static ThreadLocal<MemberEntity> loginUser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // HttpSession session = request.getSession();
        // //获取登录的用户信息
        // MemberEntity attribute = (MemberEntity) session.getAttribute(LOGIN_USER);
        // if (attribute != null) {
        //     //把登录后用户的信息放在ThreadLocal里面进行保存
        //     loginUser.set(attribute);
        //     return true;
        // } else {
        //    //未登录，返回登录页面
        //     response.setContentType("text/html;charset=UTF-8");
        //     session.setAttribute("msg", "请先进行登录");
        //     response.sendRedirect("http://127.0.0.1:8002/login.html");
        //     return false;
        // }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
